<?php
App::uses('AppModel', 'Model');
/**
 * Article Model
 *
 * @property article_categorie $article_categorie
 * @property reponses_donnee $reponses_donnee
 * @property reponse $reponse
 */
class Article extends AppModel {

	public function supprimer($id) {
		// on commence par supprimer les liens vers les autres articles
		$this->query("DELETE FROM articles_en_cascades WHERE id_article_parent = $id OR id_article_enfant = $id");
		
		// on supprime ensuite les règles qui ajoutait cet article au devis
		$this->query("DELETE quantite_articles FROM quantite_articles JOIN ajout_lignes ON ajout_lignes.id_ajout_ligne = quantite_articles.id_ajout_ligne WHERE ajout_lignes.id_article = $id");
		$this->query("DELETE declenche_ajout_lignes FROM declenche_ajout_lignes JOIN ajout_lignes ON declenche_ajout_lignes.id_ajout_ligne = ajout_lignes.id_ajout_ligne WHERE ajout_lignes.id_article = $id");
		$this->query("DELETE FROM ajout_lignes WHERE id_article = $id");
		
		// on supprime l'article en question
		$this->query("DELETE FROM articles WHERE id_article = $id");
		
		return true;
	}
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_article';
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'description_article';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_article' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'ref_article' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
						'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
						'unique' => array(
							'rule' => 'isUnique',
							'message' => 'Cette référence d\'article existe déjà.'
							
						)
		),
				'description_article' => array(
					'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
					
				),
		'tarif_brut' => array(
			'money' => array(
				'rule' => array('money'),
				'message' => 'Veuillez renseigner un prix valide.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Ce champ doit être renseigné.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasOne associations
 *
 * @var array
 */
	public $hasOne = array(
		'article_categorie' => array(
			'className' => 'articles_categorie',
			'foreignKey' => 'id_categorie',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'groupe_devis' => array(
			'className' => 'groupe_devi',
			'foreignKey' => 'id_groupe_devis',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'reponses_donnee' => array(
			'className' => 'reponses_donnee',
			'foreignKey' => 'id_article',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'reponse' => array(
			'className' => 'reponse',
			'foreignKey' => 'id_article',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

}
